package com.example.simple_file_chooser;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

public class PathUtil {
    FunCallback funCallback;
    private Context context;

    public PathUtil setFunCallback(FunCallback funCallback) {
        this.funCallback = funCallback;
        return this;
    }

    public PathUtil onCreate(TextView tv) {
        this.context = tv.getContext();

//        String options = "{\"xml\":{\"id_right_choose\":\"ok\",\"id_left_display\":\"name\",\"id_spec_choose\":\"ok\",\"id_click_cnt\":\"R.id.layout\",\"unselect\":\":invisible\",\"select\":\":visible\",\"cnt_layout\":\"com.aliang.auto.R.layout.auto_string_item_choosable_black\",\"select_default\":\"0\"},\"data\":[{\"name\":\"高德地图 (默认)\"},{\"name\":\"谷歌地图\"}]}";
//        JsonPopHandler.withCtx(this).pop(options);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.getContext().startActivity(new Intent(v.getContext(), ExplorerActivity.class).setAction("explorer").putExtra("CACHE_PATH", StorageUtil.getPrimaryStoragePath(v.getContext())));
            }
        });


        BroadcastBuilder.build().receive("explorer_out", "", context, new FunCallback() {
            @Override
            public void onCallback(Object result, Object object) {
                super.onCallback(result, object);
                String output = ((Intent) object).getStringExtra("output");
                if (funCallback != null) {
                    funCallback.onCallback(output, null);
                }
//                ((EditText) findViewById(R.id.edit)).setText(output);
            }
        });
        return this;
    }

    public void onDestroy() {
        BroadcastBuilder.remove("explorer_out", context);
    }
}
